package nexus.com.combobox
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import nexus.base.NXSprite;
	import nexus.com.list.List;
	import nexus.event.ListEvent;
	
	
	
	/**
	 * <br>설명 : </br> 
	 * @author winnie
	 */	
	public class ComboBox extends NXSprite
	{
		//-----------------------------------------------------------------------------------
		//
		// private vars
		//
		//-----------------------------------------------------------------------------------
		private var _isOpen : Boolean = false;
		private var _overIcon : MovieClip;
		private var _outIcon : MovieClip;
		private var _comboBgClip : MovieClip;
		
		private var _selectTF : TextField;
		private var _list : List; // 리스트
		
		private var _dataProvider : Array;
		private var _selectedIndex : int = 0;
		private var _selectedData : Object;
		
		private var _iconPaddingRight : Number = 5; // 아이콘의 x 좌표 설정값 ( bg 오른쪽으로부터 이격거리 )
		private var _iconPaddingTop : Number = 8; // 아이콘의 y 좌표 설정값 ( bg 상단으로부터 이격거리 )
		private var _textFieldPaddingRight : Number = 20; // 텍스트필드의 우측 이격 너비
		
		//-----------------------------------------------------------------------------------
		//
		// protected vars
		//
		//-----------------------------------------------------------------------------------
		
		
		//-----------------------------------------------------------------------------------
		//
		// public vars
		//
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// constructor
		//
		//-----------------------------------------------------------------------------------
		public function ComboBox()
		{
			super();
			
			addChild( comboBgClip );
			addChild( overIcon );
			addChild( outIcon );
			addChild( selectTF );
			
			_list = new List();
			var scrollBtn : MovieClip = new VERTICAL_SCROLL_BTN2();
			scrollBtn.x = 4;
			var scrollTrack : MovieClip = new VERTICAL_SCROLL_TRACK2();
			
			_list.verticalScroll.btn = scrollBtn;
			_list.verticalScroll.track = scrollTrack;
			_list.verticalScroll.icon = null;
			_list.padding = [1,5,0,5];
			_list.borderAllColor = 0xE4E4E4;
			_list.borderTopThickness = 1;
			_list.backGroundColor = 0xffffff;
			
			_list.addEventListener(ListEvent.LIST_SELECTION_CHANGE, onListSelectionChange );
			this.addEventListener( MouseEvent.ROLL_OVER, onOver );
			this.addEventListener( MouseEvent.ROLL_OUT, onOut );
			this.addEventListener( MouseEvent.CLICK, onClick );
			
			apply();
		}
			
		
		//-----------------------------------------------------------------------------------
		//
		// setter / getter
		//
		//-----------------------------------------------------------------------------------
		
		
		public function get textFieldPaddingRight():Number
		{
			return _textFieldPaddingRight;
		}

		public function set textFieldPaddingRight(value:Number):void
		{
			_textFieldPaddingRight = value;
		}

		public function get iconPaddingRight():Number
		{
			return _iconPaddingRight;
		}

		public function set iconPaddingRight(value:Number):void
		{
			_iconPaddingRight = value;
		}

		public function get iconPaddingTop():Number
		{
			return _iconPaddingTop;
		}

		public function set iconPaddingTop(value:Number):void
		{
			_iconPaddingTop = value;
		}

		public function get selectedData():Object
		{
			return _selectedData;
		}

		public function get selectedIndex():int
		{
			return _selectedIndex;
		}

		public function set selectedIndex(value:int):void
		{
			_selectedIndex = value;
			indexChangeControl();
		}

		public function get comboBgClip():MovieClip
		{
			if( _comboBgClip == null ) 
			{
				_comboBgClip = new COMBOBOX_BG_CLIP();
				_comboBgClip.buttonMode = true;
			}
			return _comboBgClip;
		}

		public function set comboBgClip(value:MovieClip):void
		{
			if( _comboBgClip && this.contains( _comboBgClip ) )
			{
				removeChild( _comboBgClip );
			}
			_comboBgClip = value;
			_comboBgClip.buttonMode = true;
			
		}

		public function get dataProvider():Array
		{
			return _dataProvider;
		}
		
		public function set dataProvider(value:Array):void
		{
			_dataProvider = value;
			_list.dataProvider = _dataProvider;
			_list.selectedIndex = 0;
			
			// 인덱스 리셋
			selectedIndex = 0;
		}
		
		public function get outIcon():MovieClip
		{
			if( _outIcon == null ) 
			{
				_outIcon = new COMBOBOX_ICON_OUT();
				_outIcon.mouseChildren = false;
				_outIcon.mouseEnabled = false;
			}
			return _outIcon;
		}
		
		public function set outIcon(value:MovieClip):void
		{
			if( _outIcon && this.contains( _outIcon ) )
			{
				removeChild( _outIcon );
			}
			_outIcon = value;
			_outIcon.mouseChildren = false;
			_outIcon.mouseEnabled = false;
		}
		
		public function get overIcon():MovieClip
		{
			if( _overIcon == null ) 
			{
				_overIcon = new COMBOBOX_ICON_OVER();
				_overIcon.mouseChildren = false;
				_overIcon.mouseEnabled = false;
			}
			return _overIcon;
		}
		
		public function set overIcon(value:MovieClip):void
		{
			if( _overIcon && this.contains( _overIcon ) )
			{
				removeChild( _overIcon );
			}
			
			_overIcon = value;
			_overIcon.mouseChildren = false;
			_overIcon.mouseEnabled = false;
		}
		
		public function get selectTF():TextField
		{
			if( _selectTF == null )
			{
				var tf : TextFormat = new TextFormat( "돋움", 12, 0x333333 );
				_selectTF = new TextField();
				_selectTF.autoSize = TextFieldAutoSize.LEFT;
				_selectTF.defaultTextFormat = tf;
				_selectTF.setTextFormat( tf );
				_selectTF.selectable = false;
				_selectTF.mouseEnabled = false;
				_selectTF.mouseWheelEnabled = false;
			}
			return _selectTF;
		}
		
		public function set selectTF(value:TextField):void
		{
			if( _selectTF && this.contains( _selectTF ) )
			{
				removeChild( _selectTF );
			}
			
			_selectTF = value;
			
			_selectTF.autoSize = TextFieldAutoSize.LEFT;
			_selectTF.selectable = false;
			_selectTF.mouseEnabled = false;
			_selectTF.mouseWheelEnabled = false;
		}
		
		//-----------------------------------------------------------------------------------
		//
		// public function
		//
		//-----------------------------------------------------------------------------------
		
		
		override public function updateDisplayList():void
		{
			super.updateDisplayList();
			
			
			comboBgClip.x = paddingLeft;
			comboBgClip.y = paddingTop;
			comboBgClip.width = viewWidth;
			
			overIcon.x = comboBgClip.x + comboBgClip.width - iconPaddingRight - overIcon.width;
			overIcon.y = comboBgClip.y + iconPaddingTop;
			
			outIcon.x = overIcon.x;
			outIcon.y = overIcon.y;
			
			selectTF.x = comboBgClip.x + 3;
			selectTF.y = comboBgClip.y + 2;
			
			selectTF.width = comboBgClip.width - 3 - textFieldPaddingRight;
			selectTF.height = selectTF.textHeight + 4;
			
			_list.viewWidth = comboBgClip.width-1;
			_list.viewHeight = viewHeight - comboBgClip.height;
			
		}
		
		
		//-----------------------------------------------------------------------------------
		//
		// protected function
		//
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		//
		// private function
		//
		//-----------------------------------------------------------------------------------
		
		
		private function indexChangeControl():void
		{
			selectTF.text = _list.selectedLabel;
			_selectedData = _dataProvider[ selectedIndex ];
		}
		
		
		private function openOffControl():void
		{
			if( _isOpen )
			{
				if( stage )
				{
					stage.addChild( _list );
					var ltgPt : Point = this.localToGlobal( new Point(0,0) ); 
					_list.x = ltgPt.x;
					_list.y = ltgPt.y + this.height;
					_list.apply();
				}
			}
			else
			{
				if( stage && stage.contains( _list ) )
				{
					stage.removeChild( _list );
					if( this.hitTestPoint( stage.mouseX, stage.mouseY, true ) == false )
						outIcon.visible = true;
				}
			}
		}
		
		
		//-----------------------------------------------------------------------------------
		//
		// event handler
		//
		//-----------------------------------------------------------------------------------
		
		protected function onListSelectionChange(e:ListEvent):void
		{
			selectedIndex = _list.selectedIndex;
			
			_isOpen = false;
			openOffControl();
			
			var le : ListEvent = new ListEvent( ListEvent.LIST_SELECTION_CHANGE );
			le.data = e.data;
			dispatchEvent( le );
		}		
		
		protected function onClick(e:MouseEvent):void
		{
			_isOpen = !_isOpen;
			openOffControl();
		}
		
		protected function onOut(e:MouseEvent):void
		{
			if( _isOpen == false )
				outIcon.visible = true;
		}
		
		protected function onOver(e:MouseEvent):void
		{
			outIcon.visible = false;
		}	
		
		//-----------------------------------------------------------------------------------
		//
		// destory
		//
		//-----------------------------------------------------------------------------------
		
		override public function destory():void
		{
			super.destory();
		}
	}
}




